package com.hlf.modules.api.controller;


import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.AuthIgnore;
import com.hlf.modules.sys.service.SysUserService;
import com.hlf.modules.sys.service.SysUserTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 注册
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-26 17:27
 */
@RestController
@RequestMapping("/api")
@Api("注册接口")
public class ApiRegisterController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserTokenService sysUserTokenService;

    /**
     * 注册
     */
    @SysLog("注册")
    @AuthIgnore
    @PostMapping("register")
    @ApiOperation(value = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType="string", name = "mobile", value = "手机号", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "name", value = "昵称", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "password", value = "密码", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "identifyCode", value = "验证码", required = true)
    })
    public ReturnResult register(String mobile,String name, String password ,String identifyCode){
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(name, "昵称不能为空");
        Assert.isBlank(password, "密码不能为空");
        Assert.isBlank(identifyCode, "验证码不能为空");

        Map<String, Object> map = sysUserService.saveUserAndMerchant(mobile, password,name,identifyCode);

        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }

    /**
     * 忘记密码
     */
    @SysLog("忘记密码")
    @AuthIgnore
    @PostMapping("forget")
    @ApiOperation(value = "忘记密码", notes = "忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "mobile", value = "手机号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "password", value = "密码", required = true),
            @ApiImplicitParam(paramType = "query", dataType="string", name = "identifyCode", value = "验证码", required = true)
    })
    public ReturnResult forget(String mobile, String password,String identifyCode) {
        Assert.isBlank(mobile, "手机号不能为空");
        Assert.isBlank(password, "密码不能为空");
        Assert.isBlank(identifyCode, "验证码不能为空");

        Integer i = sysUserService.forgetPassword(mobile, password,identifyCode);
        if (i == 0){
            return ReturnResult.error("密码修改失败!");
        }
        return ReturnResult.ok();
    }

    /**
     * 检验手机号是否已存在
     * @param mobile
     * @return
     */
    @AuthIgnore
    @ApiOperation(value = "检验手机号是否已存在",notes = "检验手机号是否已存在")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType="string", name = "mobile", value = "手机号", required = true)
    })
    @GetMapping("validate")
    public ReturnResult validateMobile(String mobile) {
        Assert.isBlank(mobile, "手机号不能为空");
        Integer count = sysUserService.mobileCount(mobile);
        if (count > 0){
            return ReturnResult.error("用户已经注册!");
        }
        return ReturnResult.ok();
    }


}
